﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Entity = GameStepAzure.Areas.Admin.Models;

namespace GameStepAzure.Models
{
    public class GuestBookContext
    {
        public void AgregarGuestBook(GuestBook g, string path)
        {

            using (Entity.GameStoreEntities context = new Entity.GameStoreEntities())
            {
                int idImagen = 0;
                if (context.GuestBook.ToList().Count > 0)
                {
                    idImagen = context.GuestBook.ToList().Last().Id;
                }

                string nombreImagen = g.Imagen.FileName;
                string extension = nombreImagen.Substring(nombreImagen.LastIndexOf("."));
                path += "\\" + idImagen + extension;

                Entity.GuestBook gEntity = new Entity.GuestBook() { Nombre = g.Nombre, Fecha = DateTime.Now, Comentario = g.Comentario, PathImage = path };
                context.GuestBook.AddObject(gEntity);
                context.SaveChanges();

            }
            g.Imagen.SaveAs(path);

        }

        public List<GuestBook> ListarLibroVisitas
        {
            get
            {
                List<GuestBook> libroVisitas = null;

                using (Entity.GameStoreEntities context = new Entity.GameStoreEntities())
                {

                    libroVisitas = (from g in context.GuestBook
                                    select new GuestBook()
                                    {
                                        Nombre = g.Nombre,
                                        Comentario = g.Comentario,
                                        PathImage = g.PathImage,
                                        Fecha = g.Fecha
                                    }).ToList();
                }



                return libroVisitas;
            }
        }
    }
}